import 'package:flutter/material.dart';
import 'joystick.dart';
import 'func_button.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final List<Map<String, String>> functionList = [
    {'key': 'func1', 'value': '功能 1'},
    {'key': 'func2', 'value': '功能 2'},
    {'key': 'func3', 'value': '功能 3'},
    {'key': 'func4', 'value': '功能 4'},
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('遥控车遥控器')),
        body: OrientationBuilder(
          builder: (context, orientation) {
            // 判断是否为横屏
            bool isLandscape = orientation == Orientation.landscape;

            return Row(
              children: [
                // 左侧内容（放置 Joystick）
                Expanded(
                  flex: 1,
                  child: Container(
                    color: Colors.blue,
                    child: LayoutBuilder(
                      builder: (context, constraints) {
                        double width = constraints.maxWidth;
                        double height = constraints.maxHeight;

                        return Center(
                          child: Joystick(
                            touchAreaWidth: isLandscape ? width * 0.9 : width * 0.8, // 横屏时调整大小
                            touchAreaHeight: isLandscape ? height * 0.9 : height * 0.8,
                            onDirectionChanged: (direction) {
                              print("Direction: $direction");
                            },
                          ),
                        );
                      },
                    ),
                  ),
                ),
                // 中线
                VerticalDivider(
                  color: Colors.black,
                  thickness: 2,
                  width: 2,
                ),
                // 右侧内容
                Expanded(
                  flex: 1,
                  child: Container(
                    color: Colors.green,
                    child: LayoutBuilder(
                      builder: (context, constraints) {
                        double height = constraints.maxHeight;

                        return FunctionButtonGrid(
                          functionList: functionList,
                          onButtonPressed: (String key) {
                            print('按钮点击: $key');
                          },
                          containerHeight: height,
                        );
                      },
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}